//
//  BigBackgroundViewController.m
//  QD
//
//  Created by 曾杰 on 15/10/22.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "BigBackgroundViewController.h"
#import <UIImageView+WebCache.h>
#import <FMDB.h>
#import "MQFMDBManager.h"
@interface BigBackgroundViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong)UIImageView *imageView;
@property(nonatomic, strong)UIButton *cacheButton;//收藏图片的按钮
@end

@implementation BigBackgroundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    UIScrollView *bigView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    bigView.delegate = self;
    bigView.maximumZoomScale = 2;
    bigView.minimumZoomScale = 1;
    //如果图片的高度小于屏幕的高度就出现在屏幕的中间
    if ([_model.wpic_m_height floatValue] > [[UIScreen mainScreen]bounds].size.height) {
        bigView.contentSize = CGSizeMake(MQWidth, [_model.wpic_m_height floatValue]);
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MQWidth, [_model.wpic_m_height floatValue])];
            }
    else{
        bigView.contentSize = CGSizeMake(MQWidth, [[UIScreen mainScreen]bounds].size.height);
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, MQHeight/2-[_model.wpic_m_height floatValue]/2, MQWidth, [_model.wpic_m_height floatValue])];
    }
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_model.wpic_middle]];
    [bigView addSubview:_imageView];
    [self.view addSubview:bigView];
    //收藏按钮
    _cacheButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _cacheButton.frame = CGRectMake(MQWidth-60, MQHeight-60, 40, 40);
    [_cacheButton setTitleColor:[UIColor greenColor] forState:(UIControlStateNormal)];
    [_cacheButton setTitle:@"收藏" forState:(UIControlStateNormal)];
    [_cacheButton addTarget:self action:@selector(cachePicture:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_cacheButton];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapDismis:)];
    [self.view addGestureRecognizer:tap];
}
//亲拍手势，模态消失
-(void)tapDismis:(UITapGestureRecognizer *)tap
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//确定scrollview里缩放的控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}
//缩放结束后视图调整在屏幕的中心
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    view.center = self.view.center;
}
//点击收藏后放入数据库
- (void)cachePicture:(UIButton *)button
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"加入收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
    [alert addAction:alertAction];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });
    UIImageWriteToSavedPhotosAlbum(_imageView.image, nil, nil, nil);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
